package com.minglead.springboot.platform.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.minglead.springboot.common.crud.BaseController;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.common.utils.StringUtils;
import com.minglead.springboot.platform.sys.entity.Dict;
import com.minglead.springboot.platform.sys.service.DictService;
import com.minglead.springboot.platform.sys.utils.ActionResponse;

/**
 * 字典Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public Dict get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return dictService.get(id);
		}else{
			return new Dict();
		}
	}
	
/*	@RequiresPermissions("sys:dict:view")*/
	@RequestMapping(value = {"list", ""})
	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<String> typeList = dictService.findTypeList();
		model.addAttribute("typeList", typeList);
		return "modules/sys/dictList";
	}

/*	@RequiresPermissions("sys:dict:view")*/
	@RequestMapping(value = "form")
	public String form(Dict dict, Model model) {
		model.addAttribute("dict", dict);
		return "modules/sys/dictForm";
	}

/*	@RequiresPermissions("sys:dict:edit")*/
	@RequestMapping(value = "save")//@Valid 
	public String save(Dict dict, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dict)){
			return form(dict, model);
		}
		dictService.save(dict);
		addMessage(redirectAttributes, "保存字典'" + dict.getLabel() + "'成功");
		return "redirect:" + contextPath + adminPath + "/sys/dict/?repage&type="+dict.getType();
	}
	
/*	@RequiresPermissions("sys:dict:edit")*/
	@ResponseBody
	@RequestMapping(value = "delete")
	public void delete(@Param("id") String id,HttpServletResponse response) {
		ActionResponse r = new ActionResponse(1,"操作成功！");
		if(StringUtils.isEmpty(id)){
			r.setMessage("删除失败,id不能为null");
			this.responseObject(r, response);
			return;
		}
		
		String[] _id = id.split(",");
		for(String id0:_id){
			dictService.delete(id0);
		}
		this.responseObject(r, response);
	}
	
	
	@RequestMapping(value="loadData")
	public void loadData(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String description = request.getParameter("description");
		String label = request.getParameter("label");
		String type = request.getParameter("type");
		Dict dict=new Dict();
		dict.setDescription(description);
		dict.setLabel(label);
		dict.setType(type);
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict); 
		renderGrid(request, response, page);
	}
}
